#include "system.h"
#include "wdtx_lcd5110.h"

void trig_init(void);
void trig_process(void);
void TIM2_Cap_Init(void);
void display_process(void);

uint8_t  TIM2CH1_CAPTURE_STA = 0;		    				
uint16_t TIM2CH1_CAPTURE_VAL = 0;	

int main(void)
{	
	uint64_t preTime = 0;
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
	system_init();
	trig_init();
	TIM2_Cap_Init();
	LCD5510_Init();
	LCD5110_prints(0,0,"hello");
	
	while(1)
	{
		if(get_current_time_millis() == preTime) continue;
		else preTime = get_current_time_millis();
		
		trig_process();
		display_process();
	}
}

void display_process(void)
{
	static u16 flag = 0;
	u16 distance = 0;
	u8 i = 13;
	flag++;
	if(flag != 500)return;
	flag = 0;
	/***********************/
	distance = TIM2CH1_CAPTURE_VAL/500 * 17; 
	if(distance == 0)
	{
		LCD5110_printc(i--,2,'0');
	}
	while(distance != 0)
	{
		LCD5110_printc(i--,2,distance%10+'0');
		distance/=10;
	}
}

void trig_process(void)
{
	static u8 flag = 0;
	flag++;
	if(flag != 60)return;
	flag = 0;
	/****************/
	PAout(1) = 1;
	delay_us(15);
	PAout(1) = 0;
}

void trig_init(void)
{
	RCC->APB2ENR |= 1<<2;
	GPIOA->CRL &= 0xFFFFFF0F;
	GPIOA->CRL |= 0x00000030;
	PAout(1) = 0;
}

void TIM2_Cap_Init(void)
{	
	uint16_t arr = 0xffff;
	uint16_t psc = 72 - 1;
	TIM_ICInitTypeDef  TIM2_ICInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);	
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_0);					
	
	TIM_TimeBaseStructure.TIM_Period = arr;
	TIM_TimeBaseStructure.TIM_Prescaler =psc; 
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 
	
	TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1;
  	TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  	TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  	TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 
  	TIM2_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInit(TIM2,&TIM2_ICInitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
	NVIC_Init(&NVIC_InitStructure);  
	
	TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);
	
	TIM_Cmd(TIM2,ENABLE ); 	
}

void TIM2_IRQHandler(void)
{ 
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
	{	    
		if(TIM2CH1_CAPTURE_STA == 1)
		{
			TIM2CH1_CAPTURE_VAL = 0xffff;
			TIM2CH1_CAPTURE_STA = 0;
		}
	}
	if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
	{	
		if(TIM2CH1_CAPTURE_STA == 0)
		{
	 		TIM_SetCounter(TIM2,0);
			TIM2CH1_CAPTURE_STA = 1;
			TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling);
		}
		else
		{
			TIM2CH1_CAPTURE_VAL = TIM_GetCapture1(TIM2);
			TIM2CH1_CAPTURE_STA = 0;
			TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising);
		}
	}		     	    					   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); 
}

